package com.atguigu.gmall.mq.receiver;

import com.atguigu.gmall.mq.config.DeadMqConfing;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Date;


@Component
public class DelayMessage {
    @RabbitListener(queues = DeadMqConfing.queue_delay_1)
    public void delayMessage(String msg, Message message, Channel channel){
        try {
            System.out.println("接收时间："+new Date());
            System.out.println("接收消息"+msg);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),true);
        } catch (IOException e) {
            //e.printStackTrace();
            if(!message.getMessageProperties().isRedelivered()){
                try {
                    channel.basicNack(message.getMessageProperties().getDeliveryTag(),false,true);
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }else {
                try {
                    channel.basicReject(message.getMessageProperties().getDeliveryTag(),false);
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        }
    }
}
